/**
 * 
 */
package br.com.tecnove.random.android.util;

import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * @author "Carlos Eduardo Ruhrwiem"
 * 
 */
public class AnimationUtil {

	private final static int RUNNING_VALUES = 50;
	private final static String[] SPINNING = new String[] { "-", "/", "|", "\\" };
	
	private final static Animation anim_TL = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
	private final static Animation anim_T = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
	private final static Animation anim_TR = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
	private final static Animation anim_R = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
	private final static Animation anim_BR = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
	private final static Animation anim_B = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
	private final static Animation anim_BL = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
	private final static Animation anim_L = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
	private final static Animation[] animations = new Animation[]{anim_TL, anim_T,anim_TR, anim_R, anim_BR, anim_B, anim_BL, anim_L};

	public static String[] getValuesForAnimation(int min, int max) {
		String[] returnValues = new String[RUNNING_VALUES];
		for (int i = 0; i < RUNNING_VALUES; i++) {
			returnValues[i] = String.valueOf(RandomGenerator.generateFromRange(min, max));
		}
		return returnValues;
	}

	public static String[] generateAnimationString(String[] inputArray) {
		String[] returnValue = new String[inputArray.length];

		int animationIndex = 0;
		for (int j = 0; j < inputArray.length; j++) {
			returnValue[j] = SPINNING[animationIndex] + "  " + inputArray[j] + "  " + SPINNING[animationIndex];
			animationIndex++;
		}
		
		return returnValue;
	}
	
	public static Animation getRandomDirectionAnimation(){
		return animations[RandomGenerator.generateFromRange(0, 7)];
	}
	
	/**
	 * This should be improved. But it works right now.
	 * @param values
	 * @param outView
	 * @param animatedText
	 */
	public static void animateTextView(String result, ScrollView outView, TextView animatedText) {
		animatedText.setText(result);

		AnimationSet set = new AnimationSet(true);

		Animation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(500);
		set.addAnimation(animation);

		animation = AnimationUtil.getRandomDirectionAnimation();
		animation.setDuration(1000);
		set.addAnimation(animation);

		LayoutAnimationController controller = new LayoutAnimationController(set, 0.25f);
		outView.setLayoutAnimation(controller);
	}

}
